#include "e2d/imgui/render/Editor.h"
#include "e2d/core/render/components.h"
#include "e2d/ui/Utils.h"

using Editor = e2d::BasicEditor;

template<>
bool Editor::Edit<e2d::SkinnedMeshRender>(const std::string& label, e2d::SkinnedMeshRender& e)
{
	auto& sg = e2d::State::Inst();

	int index = 0;

	e2d::CustomEditor::ComboEditEntity("m_RootNode", e.m_RootNode, e.m_Entity->m_Scene->m_AllEntitiesCalculated);

	EditInst<e2d::MaterialViewMap>("m_MaterialView", e.m_MaterialView);
	
	if (ImGui::Button("View"))
	{
		sg.m_EditMaterialFlag = true;
		sg.m_EditMaterialPath = e.m_MaterialView.m_Index.m_TargetFilePath;
	}

	EditInst<e2d::SkinnedMeshMap>("m_MeshMap", e.m_MeshMap);
	EditInst<e2d::BoneFileMap>("m_BoneFileMap", e.m_BoneFileMap);;
	CheckBoxInst<bool>("m_Rendering", e.m_Rendering);

	if (TreeNode("BoneEntities", e.m_ImguiFlag, index++))
	{
		auto& bones = e.m_BoneFileMap.m_Value;

		if (bones)
		{
			for (auto& [key, it] : bones->m_Bones)
			{
				ImGui::PushID(&it);
				auto entity = (static_cast<e2d::Entity*>(it.m_Entity));

				if (entity)
				{
					ImGui::Text("%s", entity->m_Name.c_str());
					EditInst<unsigned int>("m_BoneIndex", it.m_Index);
					EditInst<e2d::Transform>("m_BoneIndex", it.m_OffsetMatrix);
				}

				ImGui::PopID();
			};
		}

		TreePop();
	}

	if (ImGui::Button("Load"))
	{
		e.OnLoad();
	}



	return false;
}